package admin.redis.submit.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @Description: TODO
 * @Author: heky
 * @CreateDate: 2024/2/6 16:42
 */
@Slf4j
public class RequestUtils {

    private RequestUtils() {
    }

    public static String getBodyString(ServletRequest request) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;

        try {
            ServletInputStream inputStream = request.getInputStream();

            try {
                reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                String line = "";

                while((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } catch (Throwable var17) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (Throwable var16) {
                        var17.addSuppressed(var16);
                    }
                }

                throw var17;
            }

            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException var18) {
            log.warn("getBodyString出现问题！");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException var15) {
                    log.warn(ExceptionUtils.getMessage(var15));
                }
            }

        }
        return sb.toString();
    }
}
